<?php
tsLoad ( '/core/CoreLib/AbstractMysqlPage.class.php' );
class ProductPage extends AbstractMysqlPage {
	/*
	 * 取得总的行数
	 */
	protected function calTotalRows() {
		global $mysqlTools;
		$type = isset($this->condition['type'])?$this->condition['type']:null;
		$condition = $mysqlTools->check_input ( $this->condition, array (
				'keyword'
		));
		
		$sqlCore = "
            SELECT
                COUNT(*) AS totalCount
            from 
		        product p inner join sys_photo sp on p.prodCoverId = sp.photoId
            where 
                
        ";
		
		$where = " ";
	    if(isset($type)){
		    $where.="p.navType=$condition[navTypeId] ";
		}
		else if(isset($condition['prodTypeId']) && $condition['prodTypeId']!=0){
		    $where.="(p.prodTypeC1=$condition[prodTypeId] or
		    p.prodTypeC2=$condition[prodTypeId] or
		    p.prodTypeC3=$condition[prodTypeId] or
		    p.prodTypeC4=$condition[prodTypeId]  )";
		}else
    		$where.="1=1 ";
		
		//产品价格区间
		if (! empty ( $condition ['lowPrice'] ))
			$where .= ' AND p.prodNowPrice >=' . $condition ['lowPrice'];
		if (! empty ( $condition ['highPrice'] ))
		    $where .= ' AND p.prodNowPrice <=' . $condition ['highPrice'];
		//关键字查询   
		if (! empty ( $condition ['keyword'] ))
		    $where .= ' AND p.prodName like ' . $condition ['keyword'];
		
		$sql = $sqlCore . $where;
		// 总记录数
		$totalRows = $mysqlTools->selectSql ( $sql )[0]['totalCount'];
		return $totalRows;
	}
	
	/*
	 * 取得数据集合
	 */
	protected function getList($sRowNo, $pageRows) {
		global $mysqlTools;
		$sortName = isset($this->condition['sortName'])?$this->condition['sortName']:'';
		$sortType = isset($this->condition['sortType'])?$this->condition['sortType']:'';
		$type = isset($this->condition['type'])?$this->condition['type']:null;
		$condition = $mysqlTools->check_input ( $this->condition, array (
				'keyword'
		));
		
		$sqlCore = "
            SELECT
                p.prodId,
                p.prodName,
                p.prodDesc,
                p.prodPrice,
                p.prodNowPrice,
                p.prodAppraise,
                p.prodUrl,
                p.prodCoverId,
                p.prodSort,
                p.prodTypeC0,
                p.prodTypeC1,
                p.prodTypeC2,
                p.prodTypeC3,
                p.prodTypeC4,
                p.prodUserId,
                p.prodSiteId,
                p.prodCreateTime,
                p.prodModifyTime,
                p.prodState,
                p.prodSourceId,
		        p.shopName,
		        p.shopUrl,
                p.prodRecomLevel,
                sp.photoUrl
            from 
		        product p inner join sys_photo sp on p.prodCoverId = sp.photoId
            where 
        ";
		
		$where = " ";
		if(isset($type)){
		    $where.="p.navType=$condition[navTypeId] ";
		}
		else if(isset($condition['prodTypeId']) && $condition['prodTypeId']!=0){
		    $where.="(p.prodTypeC1=$condition[prodTypeId] or
		    p.prodTypeC2=$condition[prodTypeId] or
		    p.prodTypeC3=$condition[prodTypeId] or
		    p.prodTypeC4=$condition[prodTypeId]  )";
		}else
		    $where.="1=1 ";
		
		//产品价格区间
		if (! empty ( $condition ['lowPrice'] ))
			$where .= ' AND p.prodNowPrice >=' . $condition ['lowPrice'];
		if (! empty ( $condition ['highPrice'] ))
		    $where .= ' AND p.prodNowPrice <=' . $condition ['highPrice'];
		//关键字查询
		if (! empty ( $condition ['keyword'] ))
		    $where .= ' AND p.prodName like ' . $condition ['keyword'];
			
			// 排序条件
		if (! empty ( $sortName ) && ! empty ( $sortType ))
			$order = " order by p.$sortName $sortType";
		else
			$order = ' ORDER BY p.prodCreateTime ASC';
		$limit = " LIMIT $sRowNo,$pageRows";
		$sql = $sqlCore . $where . $order . $limit;
		// 查询产品分页
		$list = $mysqlTools->selectSql ( $sql );
		
		return $list;
	}
	
	/**
	 * 根据产品ID查询产品拓扑数据集
	 */
	public function queryProductTypeListByProdId($prodTypeId){
	    global $mysqlTools;
	    $condition = $mysqlTools->check_input ( $this->condition);
	    $productTypeList = array();
	    
	    $sql = "
	       SELECT
            	pt.prodTypeId,
            	pt.prodTypeName,
            	pt.prodTypeDesc,
            	pt.prodTypeCategory,
            	pt.prodTypeIndex,
            	pt.prodTypeOrder,
            	pt.prodTypeParentId,
            	pt.prodTypeCreateTime,
            	pt.prodTypeState,
            	pt.prodTypeQuantity,
            	pt.prodTypeIndexCode,
            	pt.prodTypeIsHot,
            	pt.prodTypeCateIndex
            from product_type pt
            where pt.prodTypeId = $prodTypeId
	    ";
	    
	    $list = $mysqlTools->selectSql($sql);
	    $productTypeList = array_merge($productTypeList, $list);
	    if(intval($list[0]['prodTypeParentId']) !=0){
	        $productTypeList = array_merge($productTypeList, $this->queryProductTypeListByProdId($list[0]['prodTypeParentId']));
	    }
	    return $productTypeList;
	}
	
	/**
	 * 查询导航数据
	 */
	public function queryNavList(){
		global $mysqlTools;
		$sql = "
	       SELECT
				`id`,
			    `name`,
			    `desc`
			FROM `nav_type`
			WHERE state = 1
			ORDER BY
				`sort`
			ASC
			LIMIT 0,8
	    ";
		$list = $mysqlTools->selectSql($sql);
		return $list;
	}
	
	// 查询行业类型
	public function queryProductTypeTree($typeParentId,$isHot) {
        global $mysqlTools;
        tsLoad('/manage/busi/CommonBusi.class.php');
        $typeParentId = $mysqlTools->check_input ( $typeParentId );
        
        $sql = "
        SELECT
        `prodTypeId`,
        `prodTypeName`,
        `prodTypeDesc`,
        `prodTypeCategory`,
        `prodTypeIndex`,
        `prodTypeOrder`,
        `prodTypeParentId`,
        `prodTypeCreateTime`,
        `prodTypeState`,
        `prodTypeQuantity`,
        `prodTypeIndexCode`,
        `prodTypeIsHot`,
        `prodTypeCateIndex`
        FROM `product_type`
        WHERE
            `prodTypeState` = 1";
        
        $where = '';
        
        if(!empty($isHot))
            $where .= " and `prodTypeIsHot` = $isHot";
        
        $order = " ORDER BY `prodTypeOrder` ASC";
    
        $productTypeTree = $mysqlTools->selectSql($sql.$where.$order);

        $productTypeTree = CommonBusi::getTree($productTypeTree, $typeParentId, 'prodTypeId', 'prodTypeParentId', 'child');

		return $productTypeTree;
	}
	
	/**
	 * 增加产品访问次数
	 */
	public function addVisitCount($condition) {
	    global $mysqlTools;
	    // 参数处理注入SQL安全问题
	    if(empty($condition['url']))
	        return false;
	    $url = rtrim($condition['url'], "/");
	    try {
	        $sql = "UPDATE `product` SET `visitCount` = `visitCount`+1";
	        $sql .= " WHERE `prodUrl` = '$condition[url]' OR `prodUrl` = '$url' OR `shopUrl` = '$condition[url]' OR `shopUrl` = '$url'" ;
	        $mysqlTools->executeSql($sql);
	        $sql = "UPDATE `view_simple_data` SET `visitCount` = `visitCount`+1";
	        $sql .= " WHERE `viewSimpleDataUrl` = '$condition[url]' OR `viewSimpleDataUrl` = '$url'";
	        $mysqlTools->executeSql($sql);
	        $sql = "UPDATE `view_data_floor_detail` SET `visitCount` = `visitCount`+1";
	        $sql .= " WHERE `dataFloorDetailUrl` = '$condition[url]' OR `dataFloorDetailUrl` = '$url'";
	        $mysqlTools->executeSql($sql);
	    } catch ( \Exception $e ) {
	        return false;
	    }
	    return true;
	}
}

?>